import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { getSiteStateAPI, getSettingAPI,updateSettingAPI } from "../apis/apis";

export const useCommonStore = defineStore(
  "common",
  () => {
    // 归档文章总数
    const article = ref(0);
    // 归档分类总数
    const category = ref(0);
    // 归档标签总数
    const tag = ref(0);

    const setting = ref(null);

    // 获取setting信息
    const getSetting = async () => {
      const res = await getSettingAPI();
      setting.value = res.data
    };

    // 更新setting信息 返回最新的
    const updateSetting = async (params) => {
      const res = await updateSettingAPI(params);
      setting.value = res.data
    };

    // 获取数量信息
    const getInfo = async () => {
      const res = await getSiteStateAPI();
      article.value = res.data?.articleNum;
      category.value = res.data?.categoryNum;
      tag.value = res.data.tagNum;
    };

    return {
      article,
      category,
      tag,
      getInfo,
      setting,
      getSetting,
      updateSetting
    };
  },
  {
    persist: {
      key: "common",
      storage: sessionStorage,
      // paths: ["token", "loginUserInfo"],
      serializer: {
        deserialize: JSON.parse,
        serialize: JSON.stringify,
      },
    },
  }
);
